local projectTemplate = 
{
"<?xml version='1.1' encoding='UTF-8'?>",
"<TTCodeProject id='uuid' name='Project' type='C/C++' detail='Command line tool'>",
"	<document id='uuid'>",
"		<filter id='uuid' name='Sources' type= 'source'>", 
"			<file id='uuid' name='main.c' path='./main.c'/>", 
"		</filter>", 
"		<filter id='uuid' name='Resources' type= 'resource'>", 
"		</filter>", 
"		<filter id='uuid' name='External Library' type= 'extlib'>", 
"		</filter>",
"	</document>",
"	<configs id='uuid'>",
"		<config id='uuid' name='debug'>",
"		</config>",
"		<config id='uuid' name='release'>",
"		</config>",
"	</configs>",
"	<targets id='uuid'>",
"		<target id='uuid' name='sample' type='execute'>",
"			<compile_file id='uuid' name='Compile Files'>",
"				<source_item>uuid{main.c}</source_item>",
"			</compile_file>",
"		</target>",
"	</targets>",
"	<curTarget>uuid{sample}</curTarget>",
"	<curConfig>uuid{debug}</curConfig>",
"</TTCodeProject>"
}
require("uuid")
local replacedUuidTemplate = {}
for i,v in pairs(projectTemplate) do
	local replaceIds = string.gsub(v,"id='uuid'","id='"..uuid.generate().."'")
	table.insert(replacedUuidTemplate,replaceIds)
end

local replacedContentTemplate = {}
for i,v in pairs(replacedUuidTemplate) do
--	v = string.gsub(v,"uuid(%w+)","id='"..uuid.generate().."'")
--	io.write(v.."\n")
	for word in string.gmatch(v,"uuid{(.*)}") do
--		io.write(word.."\n")
		for j,w in pairs(replacedUuidTemplate) do
			local patern = "id='(.*)'".." name='"..word.."'"
			for id in string.gmatch(w,patern) do
--				io.write(id.."\n")
				v = string.gsub(v,"uuid{(.*)}",id)
--				io.write(v.."\n")
			end
		end
	end
	table.insert(replacedContentTemplate,v)
end
local localProjectXml = table.concat(replacedContentTemplate,"\n")
if globalProjectName then
	localProjectXml = string.gsub(localProjectXml,"name='Project'","name='"..globalProjectName.."'")
end
--globalProjectPath
--io.write(localProjectXml)
local file = assert(io.open(globalProjectPath.."/"..globalProjectName..".ttcode", "w"))
file:write(localProjectXml)
file:close()

local srcTemplate = 
{
"#include <stdio.h>",
"int main(int argc,char*argv[])",
"{",
"	printf(\"Hello World\\n\");",
"	return 0;",
"}"
}
local file = assert(io.open(globalProjectPath.."/main.c", "w"))
file:write(table.concat(srcTemplate,"\n"))
file:close()

